﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AthCommander.Model;
using System.IO;

namespace AthCommander.DirectoryGrid.Dialogs
{
    public partial class RenameDialog : Form
    {
        private FileView _file;

        public RenameDialog(FileView file)
        {
            InitializeComponent();
            _file = file;
            newName.Text = _file.Name;
        }

        #region Private Helpers

        private void Rename()
        {
            DirectoryInfo root = Directory.GetParent(_file.FullName);
            string destinPath = Path.Combine(root.FullName, newName.Text);

            if (string.Compare(destinPath, _file.FullName) != 0)
            {
                if (_file.IsDirectory)
                {
                    Directory.Move(_file.FullName, destinPath);
                }
                else
                {
                    File.Move(_file.FullName, destinPath);
                }
            }
        }

        #endregion

        #region Event Handlers

        private void renameButton_Click(object sender, EventArgs e)
        {
            try
            {
                Rename();
                this.Close();
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion


    }
}
